home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 183 / dpcs0503.iso / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / wizard / WizDbManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-20  |  10.5 KB  |  379 lines

  1. package asp.wizard;
  2.  
  3. import asp.netobjects.nfx.ui.OrderedListModel;
  4. import asp.wizard.def.DefQuery;
  5. import asp.wizard.util.DataSourceDesc;
  6. import asp.wizard.util.OdbcDsnRetriever;
  7. import asp.wizard.util.UiUtil;
  8. import com.sun.java.swing.DefaultListModel;
  9. import com.sun.java.swing.ListModel;
  10. import java.lang.reflect.Array;
  11. import java.sql.Connection;
  12. import java.sql.DatabaseMetaData;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.ResultSetMetaData;
  16. import java.sql.SQLException;
  17. import java.sql.Statement;
  18. import java.util.Enumeration;
  19. import java.util.Hashtable;
  20. import java.util.Vector;
  21.  
  22. public class WizDbManager {
  23.    private static WizDbManager _instance;
  24.    private Hashtable _connectionHash = new Hashtable();
  25.    private Vector _dataSources;
  26.  
  27.    public static WizDbManager getInstance() {
  28.       if (_instance == null) {
  29.          _instance = new WizDbManager();
  30.       }
  31.  
  32.       return _instance;
  33.    }
  34.  
  35.    public static void releaseInstance() {
  36.       if (_instance != null) {
  37.          _instance.dispose();
  38.       }
  39.  
  40.       _instance = null;
  41.    }
  42.  
  43.    public void dispose() {
  44.       if (this == _instance) {
  45.          _instance = null;
  46.       }
  47.  
  48.       Connection conn = null;
  49.       Enumeration keys = this._connectionHash.keys();
  50.  
  51.       while(keys.hasMoreElements()) {
  52.          try {
  53.             conn = (Connection)this._connectionHash.get(keys.nextElement());
  54.             if (conn != null) {
  55.                conn.close();
  56.             }
  57.          } catch (SQLException e) {
  58.             System.err.println(((Throwable)e).getMessage());
  59.          }
  60.       }
  61.  
  62.       this._connectionHash.clear();
  63.       this._dataSources.removeAllElements();
  64.       this._dataSources = null;
  65.    }
  66.  
  67.    public Connection getConnection(String datasourcename, String username, String password) throws EWizDbManager {
  68.       Connection conn = null;
  69.  
  70.       try {
  71.          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  72.          datasourcename = "jdbc:odbc:" + datasourcename;
  73.          String key = datasourcename + username + password;
  74.          conn = (Connection)this._connectionHash.get(key);
  75.          if (conn == null) {
  76.             conn = DriverManager.getConnection(datasourcename, username, password);
  77.             this._connectionHash.put(key, conn);
  78.          }
  79.  
  80.          return conn;
  81.       } catch (Exception e) {
  82.          throw new EWizDbManager(((Throwable)e).getMessage());
  83.       }
  84.    }
  85.  
  86.    public ResultSet getResultSet(Connection conn, String SQL) throws EWizDbManager {
  87.       ResultSet rs = null;
  88.  
  89.       try {
  90.          Statement stmt = conn.createStatement();
  91.          rs = stmt.executeQuery(SQL);
  92.          return rs;
  93.       } catch (Exception e) {
  94.          throw new EWizDbManager(((Throwable)e).getMessage());
  95.       }
  96.    }
  97.  
  98.    public DatabaseMetaData getDatabaseMetaData(String datasourcename, String username, String password) throws EWizDbManager {
  99.       DatabaseMetaData wizDbMetaData = null;
  100.  
  101.       try {
  102.          wizDbMetaData = this.getConnection(datasourcename, username, password).getMetaData();
  103.          return wizDbMetaData;
  104.       } catch (Exception e) {
  105.          throw new EWizDbManager(((Throwable)e).getMessage());
  106.       }
  107.    }
  108.  
  109.    public static ResultSetMetaData getResultSetMetaData(ResultSet rs) throws EWizDbManager {
  110.       ResultSetMetaData wizRSMetaData = null;
  111.  
  112.       try {
  113.          wizRSMetaData = rs.getMetaData();
  114.          return wizRSMetaData;
  115.       } catch (Exception e) {
  116.          throw new EWizDbManager(((Throwable)e).getMessage());
  117.       }
  118.    }
  119.  
  120.    public static OrderedListModel getTables(Connection conn, boolean excludeContainingSpace, boolean[] containingSpaceFound) throws EWizDbManager {
  121.       OrderedListModel lm = new OrderedListModel();
  122.       if (containingSpaceFound != null && containingSpaceFound.length > 0) {
  123.          containingSpaceFound[0] = false;
  124.       }
  125.  
  126.       try {
  127.          DatabaseMetaData metaData = conn.getMetaData();
  128.          String[] tableTypes = new String[]{"TABLE"};
  129.          ResultSet rs = metaData.getTables((String)null, (String)null, (String)null, tableTypes);
  130.          String tableName = null;
  131.  
  132.          while(rs.next()) {
  133.             tableName = rs.getString(3);
  134.             if (excludeContainingSpace && tableName.indexOf(" ") != -1) {
  135.                if (containingSpaceFound != null && containingSpaceFound.length > 0) {
  136.                   containingSpaceFound[0] = true;
  137.                }
  138.             } else {
  139.                lm.addElement(tableName);
  140.             }
  141.          }
  142.  
  143.          rs.close();
  144.          return lm;
  145.       } catch (SQLException e) {
  146.          String msg = "Could not get table list from the database: " + ((Throwable)e).getMessage();
  147.          throw new EWizDbManager(msg);
  148.       }
  149.    }
  150.  
  151.    public static ListModel getTables(ResultSet rs) throws EWizDbManager {
  152.       OrderedListModel lm = new OrderedListModel();
  153.       String tablename = "";
  154.  
  155.       try {
  156.          ResultSetMetaData rsMetaData = rs.getMetaData();
  157.  
  158.          for(int i = 1; i <= rsMetaData.getColumnCount(); ++i) {
  159.             tablename = rsMetaData.getTableName(i);
  160.             Object[] result = lm.find(tablename);
  161.             if ((Boolean)result[0]) {
  162.                lm.addElement(tablename);
  163.             }
  164.          }
  165.  
  166.          return lm;
  167.       } catch (Exception e) {
  168.          throw new EWizDbManager("Could not get fields list from the recordset." + ((Throwable)e).getMessage());
  169.       }
  170.    }
  171.  
  172.    public static ListModel getFields(Connection conn, String tableName, DefaultListModel lm, boolean excludeContainingSpace, boolean[] containingSpaceFound) throws EWizDbManager {
  173.       if (lm == null) {
  174.          return null;
  175.       } else {
  176.          if (containingSpaceFound != null && containingSpaceFound.length > 0) {
  177.             containingSpaceFound[0] = false;
  178.          }
  179.  
  180.          try {
  181.             DatabaseMetaData metaData = conn.getMetaData();
  182.             String[] var10000 = new String[]{"TABLE"};
  183.             ResultSet rs = metaData.getColumns((String)null, (String)null, tableName, (String)null);
  184.             String fieldName = null;
  185.  
  186.             while(rs.next()) {
  187.                fieldName = rs.getString(4);
  188.                if (excludeContainingSpace && fieldName.indexOf(" ") != -1) {
  189.                   if (containingSpaceFound != null && containingSpaceFound.length > 0) {
  190.                      containingSpaceFound[0] = true;
  191.                   }
  192.                } else {
  193.                   lm.addElement(fieldName);
  194.                }
  195.             }
  196.  
  197.             rs.close();
  198.             return lm;
  199.          } catch (Throwable var9) {
  200.             throw new EWizDbManager("Could not get fields list from the database.");
  201.          }
  202.       }
  203.    }
  204.  
  205.    public static ListModel getFields(ResultSet rs, boolean showTableName, DefaultListModel lm) throws EWizDbManager {
  206.       if (lm == null) {
  207.          return null;
  208.       } else {
  209.          String tablename = "";
  210.  
  211.          try {
  212.             ResultSetMetaData rsMetaData = rs.getMetaData();
  213.  
  214.             for(int i = 1; i <= rsMetaData.getColumnCount(); ++i) {
  215.                if (showTableName) {
  216.                   tablename = rsMetaData.getTableName(i) + ".";
  217.                }
  218.  
  219.                lm.addElement(tablename + rsMetaData.getColumnName(i));
  220.             }
  221.  
  222.             return lm;
  223.          } catch (Exception e) {
  224.             throw new EWizDbManager("Could not get fields list from the recordset." + ((Throwable)e).getMessage());
  225.          }
  226.       }
  227.    }
  228.  
  229.    public static ListModel getFields(DefQuery defQuery, boolean showTableName, DefaultListModel lm) throws EWizDbManager {
  230.       if (lm == null) {
  231.          return null;
  232.       } else {
  233.          String tablename = "";
  234.          Vector rsColumnInfos = defQuery.getResultSetColumnInfos();
  235.          ResultSetColumnInfo rsci = null;
  236.  
  237.          for(int i = 0; i < rsColumnInfos.size(); ++i) {
  238.             rsci = (ResultSetColumnInfo)rsColumnInfos.elementAt(i);
  239.             if (showTableName) {
  240.                tablename = rsci.getTableName() + ".";
  241.             }
  242.  
  243.             lm.addElement(tablename + rsci.getColumnName());
  244.          }
  245.  
  246.          return lm;
  247.       }
  248.    }
  249.  
  250.    public static ListModel getFields(ResultSet rs, String TableName, boolean showTableName, DefaultListModel lm) throws EWizDbManager {
  251.       if (lm == null) {
  252.          return null;
  253.       } else {
  254.          String tablename = "";
  255.  
  256.          try {
  257.             ResultSetMetaData rsMetaData = rs.getMetaData();
  258.  
  259.             for(int i = 1; i <= rsMetaData.getColumnCount(); ++i) {
  260.                if (rsMetaData.getTableName(i).compareTo(TableName) == 0) {
  261.                   if (showTableName) {
  262.                      tablename = TableName + ".";
  263.                   }
  264.  
  265.                   lm.addElement(tablename + rsMetaData.getColumnName(i));
  266.                }
  267.             }
  268.  
  269.             return lm;
  270.          } catch (Exception e) {
  271.             throw new EWizDbManager("Could not get fields list from the recordset." + ((Throwable)e).getMessage());
  272.          }
  273.       }
  274.    }
  275.  
  276.    public static int getDBTypes(DefQuery defQuery, String fieldName) throws EWizDbManager {
  277.       int result = 0;
  278.       Vector rsColumnInfos = defQuery.getResultSetColumnInfos();
  279.       if (rsColumnInfos != null) {
  280.          ResultSetColumnInfo rsci = null;
  281.          boolean found = false;
  282.  
  283.          for(Enumeration e = rsColumnInfos.elements(); e.hasMoreElements() && !found; found = rsci.getColumnName().equals(fieldName)) {
  284.             rsci = (ResultSetColumnInfo)e.nextElement();
  285.          }
  286.  
  287.          if (found) {
  288.             result = rsci.getColumnType();
  289.          }
  290.       }
  291.  
  292.       return result;
  293.    }
  294.  
  295.    public Vector getDataSources() {
  296.       if (this._dataSources == null) {
  297.          DataSourceDesc[] dsnList = OdbcDsnRetriever.getDsnList();
  298.          Vector dslist = new Vector();
  299.          DataSourceDesc dsd = null;
  300.          boolean isSqlServer = false;
  301.          boolean isAccess = false;
  302.  
  303.          for(int i = 0; i < Array.getLength(dsnList); ++i) {
  304.             dsd = dsnList[i];
  305.             isSqlServer = dsd.getType().indexOf("SQL Server") != -1;
  306.             isAccess = dsd.getType().indexOf("Access") != -1;
  307.             if (isAccess || isSqlServer) {
  308.                dslist.addElement(dsd);
  309.             }
  310.          }
  311.  
  312.          OrderedListModel olm = new OrderedListModelCaseInsensitive();
  313.          int count = dslist.size();
  314.  
  315.          for(int i = 0; i < count; ++i) {
  316.             olm.addElement(dslist.elementAt(i));
  317.          }
  318.  
  319.          olm.setOrdered(true);
  320.          dslist.removeAllElements();
  321.  
  322.          for(int i = 0; i < count; ++i) {
  323.             dslist.addElement(((DefaultListModel)olm).getElementAt(i));
  324.          }
  325.  
  326.          this._dataSources = dslist;
  327.       }
  328.  
  329.       return this._dataSources;
  330.    }
  331.  
  332.    public static String getDataTypeName(int typeVal) {
  333.       String result = "";
  334.       if (typeVal != -5 && typeVal != 3 && typeVal != 6 && typeVal != 2 && typeVal != 7 && typeVal != 5 && typeVal != 4) {
  335.          if (typeVal != 1 && typeVal != -1 && typeVal != 12) {
  336.             if (typeVal != -6 && typeVal != -7) {
  337.                if (typeVal == 91 || typeVal == 93) {
  338.                   result = "date";
  339.                }
  340.             } else {
  341.                result = "boolean";
  342.             }
  343.          } else {
  344.             result = "string";
  345.          }
  346.       } else {
  347.          result = "number";
  348.       }
  349.  
  350.       return result;
  351.    }
  352.  
  353.    public static Vector getInternalDataTypes() {
  354.       Vector result = UiUtil.strToVector("string|number|boolean|date");
  355.       return result;
  356.    }
  357.  
  358.    public static Vector getResultSetColumnInfosFrom(ResultSet rs) throws EWizDbManager {
  359.       Vector result = new Vector();
  360.  
  361.       try {
  362.          ResultSetMetaData rsMetaData = rs.getMetaData();
  363.          ResultSetColumnInfo rsci = null;
  364.  
  365.          for(int i = 1; i <= rsMetaData.getColumnCount(); ++i) {
  366.             rsci = new ResultSetColumnInfo();
  367.             rsci.setTableName(rsMetaData.getTableName(i));
  368.             rsci.setColumnName(rsMetaData.getColumnName(i));
  369.             rsci.setColumnType(rsMetaData.getColumnType(i));
  370.             result.addElement(rsci);
  371.          }
  372.  
  373.          return result;
  374.       } catch (Exception e) {
  375.          throw new EWizDbManager("Could not get fields info from the recordset." + ((Throwable)e).getMessage());
  376.       }
  377.    }
  378. }
  379.